//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package com.marakana.obolo;

import game.Human;
import game.IAHard;
import game.IANormal;
import game.Player;
import graphics.PackageRulesManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SinglePlayerActivity extends Activity {
	private SharedPreferences prefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.single_player);

		TextView toContinue = (TextView) findViewById(R.id.txtContinue);
		toContinue.setVisibility(TextView.INVISIBLE);

		TextView txtNewGame = (TextView) findViewById(R.id.txtQuickGame);
		TextView story = (TextView) findViewById(R.id.txtStory);
		TextView txtBack = (TextView) findViewById(R.id.txtBack);

		txtNewGame.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Accedeix a les preferencias del joc
				prefs = PreferenceManager.getDefaultSharedPreferences(v
						.getContext());

				// Crea els jugadors segons la modalitat del joc
				Player[] players = createPlayersMode();

				// Seteja la dimensio del taulell i les normes del joc
				int[] dimensionBoard;
				String[] rules;

				String[] dimensionPrefs = prefs.getString("boardSize", "3x3")
						.split("x");
				// Dimensio del taulell
				dimensionBoard = new int[] {
						Integer.valueOf(dimensionPrefs[0]),
						Integer.valueOf(dimensionPrefs[1]), 0 };
				// Les normes de la partida
				rules = prefs.getString("rules", "upward").split(
						PackageRulesManager.SEPARATOR);

				// S'empaqueta la informaci� per pasar-la atrav�s d'un intent a
				// la seguent activity
				Intent newGame = new Intent(v.getContext(),
						SelectDeckActivity.class);
				newGame.putExtra("dimension", dimensionBoard);
				newGame.putExtra("rules", rules);
				newGame.putExtra("user", players[0]);
				newGame.putExtra("rival", players[1]);
				// Assigna la seleccio del primer deck
				// int selectedPlayer = 1;
				newGame.putExtra("selectedplayer", new String("user"));
				startActivity(newGame);
			}
		});

		story.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent story_creator_activity = new Intent(v.getContext(),
						StoryCreatorActivity.class);
				startActivity(story_creator_activity);
			}
		});

		txtBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

	}

	private Player[] createPlayersMode() {
		Player user, rival;

		// Obte les preferencias cargades en la memoria del mobil
		prefs = PreferenceManager.getDefaultSharedPreferences(this);

		// Crea els dos jugadors
		switch (Integer.valueOf(prefs.getString("numPlayers", "1"))) {
		case 1:
			user = new Human(prefs.getString("userName", "User"));
			if (prefs.getString("dificulty", "1").equals("1"))
				rival = new IANormal("Android");
			else
				rival = new IAHard("Android");

			break;

		case 2:
			user = new Human(prefs.getString("userName", "User"));
			rival = new Human(prefs.getString("rivalName", "Rival"));
			break;

		case 3:
			user = new IANormal(prefs.getString("userName", "User"));
			rival = new IAHard(prefs.getString("rivalName", "Rival"));

			break;

		default:
			user = new Human("Guest");
			rival = new IANormal("Android");
			break;
		}
		Player[] players = new Player[] { user, rival };
		return players;
	}
}
